# -*- coding:utf-8 -*-
"""
@project：Spider
@author: Nero
@file: weibo.py
@time: 2021/4/22 10:59
"""
import json
from abc import ABC

import scrapy

from opinionSpider.items import WbItem


class WeiboSpider(scrapy.Spider, ABC):
    name = 'weibo'

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self._index = "https://weibo.com/ajax/feed/hottimeline?since_id=0&refresh=0&group_id=102803&containerid" \
                      "=102803&extparam=discover%7Cnew_feed&max_id=0&count=30 "

        self._hot = "https://m.weibo.cn/api/feed/trendtop?containerid=102803_ctg1_8999_-_ctg1_8999_home"
        self._extend = "https://m.weibo.cn/statuses/extend?id={id}"

        self._comment = "https://m.weibo.cn/comments/hotflow?id={id}&mid={mid}"

        self._top = "https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1" \
                    "%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=seat%3D1%26pos" \
                    "%3D0_0%26mi_cid%3D100103%26cate%3D10103%26filter_type%3Drealtimehot%26c_type%3D30%26display_time" \
                    "%3D1612419453&luicode=10000011&lfid=231583"

        self._hotword = "https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot" \
                        "%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=seat" \
                        "%3D1%26pos%3D0_0%26mi_cid%3D100103%26cate%3D10103%26filter_type%3Drealtimehot%26c_type%3D30" \
                        "%26display_time%3D1612419453&luicode=10000011&lfid=231583"

    def start_requests(self):
        yield scrapy.Request(self._index, callback=self.parse_index)

    # def parse_hot(self, response):
    #     hot_json = json.loads(response.text)
    #     if hot_json['ok'] != 1:
    #         return
    #     datas = hot_json['data']['statuses']
    #
    #     for data in datas:
    #         wbItem = WbItem()
    #
    #         wbItem['id'] = data['id']
    #         wbItem['data'] = data
    #
    #         yield scrapy.Request(self._extend.format(id=wbItem['id']), callback=self.parse_extend,
    #                              meta={'item': wbItem})

    def parse_index(self, response):
        index_json = json.loads(response.text)
        if index_json['ok'] != 1:
            return
        wbItem = WbItem()

        wbItem['data'] = index_json['statuses']

        yield wbItem

    # def parse_comment(self, response):
    #     wbItem: WbItem = response.meta['item']
    #     comment_json = json.loads(response.text)
    #     if comment_json['ok'] != 1:
    #         return
    #     wbItem['comment'] = comment_json['data']
    #
    #     yield wbItem

    # def parse_extend(self, response):
    #     wbItem: WbItem = response.meta['item']
    #     extend_json = json.loads(response.text)
    #     if extend_json['ok'] != 1:
    #         return
    #     wbItem['extend'] = extend_json['data']
    #
    #     yield scrapy.Request(self._comment.format(id=wbItem['id'], mid=wbItem['id']), callback=self.parse_comment,
    #                          meta={'item': wbItem})
